package com.lianniu.web.seller.controller;

import com.lianniu.web.base.BaseResult;
import com.lianniu.web.baseService.user.model.FrontUserVo;
import com.lianniu.web.baseService.user.service.IFrontUserService;
import com.lianniu.web.baseService.user.service.ISupplyService;
import com.lianniu.web.baseService.user.service.impl.FrontUserServiceImpl;
import com.lianniu.web.buyerProduct.service.IBuyerInfoService;
import com.lianniu.web.seller.service.ISupplyRegisterService;
import com.lianniu.web.seller.vo.Supply;
import com.lianniu.web.util.MD5Util;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * Created by Administrator on 2017-05-08.
 */
@Controller
@RequestMapping("/rest/supply/register")
public class SupplyRegisterRestController {
    @Autowired
    private IFrontUserService frontUserService;
    @Autowired
    private ISupplyService supplyService;

    @Autowired
    private IBuyerInfoService buyerInfoService;
    @Autowired
    private ISupplyRegisterService supplyRegisterService;

    @RequestMapping("/getUser")
    @ResponseBody
    public BaseResult getUser(String mobile) {
        BaseResult user = frontUserService.getUserByUserName(mobile);
        if (user.getData() == null) {
            return BaseResult.success();
        }
        Supply supply =(Supply) supplyRegisterService.selectByUserId(((FrontUserVo) user.getData()).getUsersId().toString()).getData();
        if (supply != null) {
            return BaseResult.fail("0001", "已有卖家");
        }
        BaseResult buyer = buyerInfoService.getBuyerInfoByUserId(((FrontUserVo) user.getData()).getUsersId().toString());
        if (buyer.getData() != null) {
            return BaseResult.fail("0002","已有买家");
        }
        return BaseResult.success();
    }

    @RequestMapping("/selectByUserId")
    @ResponseBody
    public BaseResult selectByUserId(String userId) {

        return supplyRegisterService.selectByUserId(userId);
    }
    @RequestMapping("/register")
    @ResponseBody
    public BaseResult register(HttpServletRequest request, Supply supply) {
        FrontUserVo frontUserVo = new FrontUserVo();
        String password = request.getParameter("loginPassWord");
        String mobile = request.getParameter("mobile");
        frontUserVo.setMobile(mobile);
        if (password != null) {
            String loginPassWord = MD5Util.getMD5String(password);
            frontUserVo.setLoginPassWord(loginPassWord);
        }
        frontUserVo.setDelFlag("1");
        frontUserVo.setUserName(mobile);
        frontUserVo.setCategoryId("2");
        frontUserVo.setLiveProvince(supply.getProvince());
        frontUserVo.setLiveCity(supply.getCity());
        frontUserVo.setLiveCounty(supply.getCounty());
        BaseResult baseResult = frontUserService.insertFrontUser(frontUserVo);
        supply.setUserid(frontUserVo.getUsersId());
        supply.setDelflag("1");
        supply.setResmobile(mobile);
        if (StringUtils.isBlank(supply.getBankname())) {
            supply.setBankname(supply.getSupplyname());
        }
        supply.setAddtime(new Date());
        supply.setSupplystatus("1");
        BaseResult supplyInsert = supplyRegisterService.insertSelective(supply);
        return BaseResult.success(supplyInsert);
    }
}
